package org.dreamwork.assistant.tools.cli;

import org.dreamwork.util.StringUtil;

import java.io.*;

/**
 * Created by game on 2018/10/13
 */
public abstract class Script {
    protected Writer writer;
    protected String CRLF = "\r\n";

    protected Script (Writer writer) {
        this.writer = writer;
    }

    protected Script (File out) throws IOException {
        writer = new FileWriter (out);
    }

    protected void println () throws IOException {
        writer.write (CRLF);
    }

    public void close () {
        if (writer != null) {
            try {
                writer.flush ();
            } catch (IOException ex) {
                // ignore
            }

            try {
                writer.close ();
            } catch (IOException ex) {
                // ignore too
            }
        }
    }

    public abstract void generate (Env env) throws IOException;

    public static final class WinScript extends Script {
        private boolean debug;
        public WinScript (File out, boolean debug) throws IOException {
            super (out);
            this.debug = debug;
        }

        @Override
        public void generate (Env env) throws IOException {
            writer.write ("@echo off"); println ();
            writer.write ("SET MAIN_CLASS=" + env.mainClass); println ();
            if (!StringUtil.isEmpty (env.javaHome))
            writer.write ("SET JAVA_HOME=" + env.javaHome); println ();
            if (debug) {
                writer.write ("SET JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + env.debug_port);
                println ();
            }
            writer.write ("FOR %%i IN (..\\libs\\*.jar) DO CALL set_env.bat %%i"); println ();
            writer.write ("%JAVA_HOME%\\bin\\java %JAVA_OPTS% -cp %CP% %MAIN_CLASS% %*");

            File file = new File (env.base, "bin/set_env.bat");
            Writer fw = new FileWriter (file);
            try {
                fw.write ("SET CP=%CP%;%1");
                fw.flush ();
            } finally {
                try {
                    fw.close ();
                } catch (IOException ex) {
                    // ignore
                }
            }
        }
    }

    public static final class LinuxScript extends Script {
        private boolean debug;
        protected LinuxScript (File out, boolean debug) throws IOException {
            super (out);
            CRLF = "\n";
            this.debug = debug;
        }

        @Override
        public void generate (Env env) throws IOException {
            writer.write ("#!/bin/bash");                   println ();
            writer.write ("MAIN_CLASS=" + env.mainClass);   println ();
            if (!StringUtil.isEmpty (env.javaHome))
            writer.write ("JAVA_HOME=" + env.javaHome);     println ();
            writer.write ("for i in ../libs/*.jar; do");    println ();
            writer.write ("    CP=$CP:$i");                 println ();
            writer.write ("done");                          println ();
            if (debug) {
                writer.write ("JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + env.debug_port);
                println ();
            }

            writer.write ("$JAVA_HOME/bin/java $JAVA_OPTS -cp $CP $MAIN_CLASS $*");
            if (!debug) {
                writer.write (" > ../logs/output.log 2>&1 &");
            }
            println ();
        }
    }

    public static final class Env {
        String  mainClass;
        String  javaHome;
        String  outDir;
        String  pom;
        String  incDir;
        String  configFile;
        int     debug_port;
        File    base;
    }
}